package com.kavian.probability.collect.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.kavian.common.util.UuidUtil;
import com.kavian.probability.collect.domain.ColorBall;
import com.kavian.test.db.DBUtil;

/**
 * 爬虫持久层
 * 
 * @author kavian
 * 
 */
public class CollectDao {

	public CollectDao() {
	}

	/**
	 * 期号查询对象是否存在
	 * 
	 * @param issue
	 *            期号
	 * @return
	 */
	public ColorBall findColorBall(String issue) {
		try {
			DBUtil dbUtil = new DBUtil();
			String sql = "SELECT issue FROM color_ball WHERE issue='" + issue
					+ "'";

			ResultSet rs = dbUtil.sendQuery(sql);

			List<ColorBall> list = new ArrayList<ColorBall>();
			if (null != rs) {
				ColorBall ball;
				while (rs.next()) {
					ball = new ColorBall();
					ball.setIssue(rs.getString("issue"));
					list.add(ball);
				}
			}

			if (null != list && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 添加数据
	 * 
	 * @param colorBall
	 */
	public void saveColorBall(ColorBall colorBall) {
		DBUtil dbUtil = new DBUtil();
		String sql = "INSERT INTO color_ball(id,issue,lottery_results_red,lottery_results_blue) VALUES('"
				+ UuidUtil.getUuid()
				+ "','"
				+ colorBall.getIssue()
				+ "','"
				+ colorBall.getLotteryResultsRed()
				+ "','"
				+ colorBall.getLotteryResultsBlue() + "')";
		dbUtil.sendUpdate(sql);

	}

	/**
	 * 返回总数
	 * @return
	 */
	public int findTotal() {
		try {
			DBUtil dbUtil = new DBUtil();
			String sql = "SELECT COUNT(*) as num FROM color_ball";

			ResultSet rs = dbUtil.sendQuery(sql);
			if(rs.next()){
				int total = rs.getInt("num");
				return total;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 返回所有双色球数据
	 * @return
	 */
	public List<ColorBall> searchCollect() {
		try {
			DBUtil dbUtil = new DBUtil();
			String sql = "SELECT * FROM color_ball";

			ResultSet rs = dbUtil.sendQuery(sql);

			List<ColorBall> list = new ArrayList<ColorBall>();
			if (null != rs) {
				ColorBall ball;
				while (rs.next()) {
					ball = new ColorBall();
					ball.setIssue(rs.getString("issue"));
					ball.setIssue(rs.getString("lottery_results_red"));
					ball.setIssue(rs.getString("lottery_results_blue"));
					list.add(ball);
				}
			}

			if(null != list && list.size() >  0){
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 查找单个球的出现次数
	 * @param num
	 */
	public int findRedBallCount(String num) {
		try {
			DBUtil dbUtil = new DBUtil();
			String sql = "SELECT COUNT(*) as num FROM color_ball WHERE lottery_results_red LIKE '%"+num+"%'";

			ResultSet rs = dbUtil.sendQuery(sql);
			if(rs.next()){
				int total = rs.getInt("num");
				return total;
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int findBlueBallCount(String num) {
		try {
			DBUtil dbUtil = new DBUtil();
			String sql = "SELECT COUNT(*) as num FROM color_ball WHERE lottery_results_blue = '"+num+"'";

			ResultSet rs = dbUtil.sendQuery(sql);
			if(rs.next()){
				int total = rs.getInt("num");
				return total;
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}